#include "Alice/Core/World.h"
#include "Alice/Log/Log.h"

namespace Alice
{

World::World()
{
    m_entity_mngr = std::make_unique<EntityMngr>();
    m_system_mngr = std::make_unique<SystemMngr>();
    m_event_mngr = std::make_unique<EventMngr>();
    // 默认空场景
    m_current_scene = "ALICE_NONE_SCENE";
    m_scenes.insert({m_current_scene, Scene()});
}

Entity World::CreateEntity()
{
    return m_entity_mngr->CreateEntity();
}

void World::DestroyEntity(Entity entity)
{
    m_entity_mngr->DestroyEntity(entity);
    Signature sig = GetEntitySignature(entity);
    m_system_mngr->UpdateEntitys(entity, sig);
}

Signature World::GetEntitySignature(Entity entity)
{
    return m_entity_mngr->GetSignature(entity);
}

std::set<EntityId> World::GetEntitys(Signature signature)
{
    return m_entity_mngr->GetEntitys(signature);
}

void World::Before()
{
    m_system_mngr->Before();
}

void World::Update()
{
    m_timer.Clock();
    m_system_mngr->Update();
    m_event_mngr->CheckTimerEvent(m_timer);
}

void World::After()
{
    m_system_mngr->After();
}

void World::InitQuadTree(WindowInfo& window_info)
{
    m_quad_tree.Init(this, 10);
    m_quad_tree.Create(0, 0, window_info.width, window_info.height);
}

void World::AddToScene(std::string scene_name, Entity entity)
{
    if (m_scenes.find(scene_name) != m_scenes.end())
    {
        m_scenes[ scene_name ].entitys.insert(entity);
    }
}

void World::CreateScene(std::string scene_name)
{
    m_scenes.insert({scene_name, Scene()});
}

void World::StartScene(std::string scene_name)
{
    m_current_scene = scene_name;
}

Scene& World::GetCurrentScene()
{
    return m_scenes[ m_current_scene ];
}

} // namespace Alice
